package com.weplay.gameplane;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.weplay.gameplane.utils.SPUtils;
import com.weplay.gameplane.utils.ScreenUtils;


public class MainActivity extends Activity {

    private FrameLayout splash_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        splash_container = findViewById(R.id.splash_container);

//        splash_container.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                if (200 == SPUtils.getInstance(MainActivity.this).getInt("isAuthentication", 0)) {
//                    startGame();
//                } else {
//                    starAuthentication();
//                }
//                MainActivity.this.finish();
//
//            }
//        },2000);


//        TTAdSdk.getAdManager().requestPermissionIfNecessary(BaseApplication.getAppContext());
        //创建TTAdNative对象，createAdNative(Context context) context需要传入Activity对象
        TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(this);


        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("888288840")
                //不区分渲染方式，要求开发者同时设置setImageAcceptedSize（单位：px）
                // 和setExpressViewAcceptedSize（单位：dp ）接口，不同时设置可能会导致展示异常。
                .setImageAcceptedSize(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight())
                .setExpressViewAcceptedSize(ScreenUtils.px2dp(this, ScreenUtils.getScreenWidth()),
                        ScreenUtils.px2dp(this, ScreenUtils.getScreenHeight()))
                .setAdLoadType(TTAdLoadType.PRELOAD)
                //推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build();


        int AD_TIME_OUT = 10 * 1000;

        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.CSJSplashAdListener() {

            //开屏素材加载成功
            @Override
            public void onSplashLoadSuccess() {
                Log.d("MA", "onSplashLoadSuccess");
            }

            //加载开屏素材失败
            @Override
            public void onSplashLoadFail(CSJAdError error) {
                //开发者处理跳转到APP主页面逻辑
                Log.d("MA", "loadSplashAd OnError" + error.getMsg() + " code:" + error.getCode());
                if (200 == SPUtils.getInstance(MainActivity.this).getInt("isAuthentication", 0)) {
                    startGame();
                } else {
                    starAuthentication();
                }
                MainActivity.this.finish();
            }

            //开屏渲染成功，可以展示开屏
            @Override
            public void onSplashRenderSuccess(CSJSplashAd ad) {

                if (ad == null) {
                    return;
                }
                Log.e("MA", "onSplashRenderSuccess");
                if (splash_container != null && !MainActivity.this.isFinishing()) {
                    splash_container.removeAllViews();
                    //把SplashView 添加到ViewGroup中,注意开屏广告view：width =屏幕宽；height >=75%屏幕高
                    ad.showSplashView(splash_container);
                    //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置，您需要自定义倒计时逻辑
                    //ad.setNotAllowSdkCountdown();
                } else {
                    //开发者处理跳转到APP主页面逻辑
                    if (200 == SPUtils.getInstance(MainActivity.this).getInt("isAuthentication", 0)) {
                        startGame();
                    } else {
                        starAuthentication();
                    }
                    MainActivity.this.finish();
                }
                ad.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
                    @Override
                    public void onSplashAdShow(CSJSplashAd csjSplashAd) {

                    }

                    @Override
                    public void onSplashAdClick(CSJSplashAd csjSplashAd) {

                    }

                    @Override
                    public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                        //开发者处理跳转到APP主页面逻辑
                        if (200 == SPUtils.getInstance(MainActivity.this).getInt("isAuthentication", 0)) {
                            startGame();
                        } else {
                            starAuthentication();
                        }
                        MainActivity.this.finish();

                    }
                });

                if (ad == null) {
                    return;
                }

            }

            @Override
            public void onSplashRenderFail(CSJSplashAd ad, CSJAdError csjAdError) {
                //开发者处理跳转到APP主页面逻辑

            }
        }, AD_TIME_OUT);

    }


    public void startGame() {
        Intent intent = new Intent(this, GameActivity.class);
        startActivity(intent);
    }

    public void starAuthentication() {
        Intent intent = new Intent(this, AuthenticationActivity.class);
        startActivity(intent);
    }


}